{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "因为要根据关键词“氢”筛选企业，首先要找出有哪些“氢化，氢氧，化氢”等无关词语"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_438370/3198777542.py:4: DtypeWarning: Columns (10,16,18,22,31) have mixed types. Specify dtype option on import or set low_memory=False.\n",
      "  df = pd.read_csv(\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "file_name = \"氢能_名称_经营范围_867w.csv\"\n",
    "df = pd.read_csv(\n",
    "    file_name,\n",
    "    # nrows=10000\n",
    "    )"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 构建氢能黑名单"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def find_kw(item):\n",
    "    if not isinstance(item, str):\n",
    "        return []\n",
    "    indices_of_ones = [i for i, char in enumerate(item) if char == '氢']\n",
    "    n = len(item)\n",
    "    res = []\n",
    "    for idx in indices_of_ones:\n",
    "        bottom = idx - 3\n",
    "        top = idx + 4\n",
    "        bottom = max(0, bottom)\n",
    "        top = min(top, n)\n",
    "        res.append(item[bottom:top])\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_kw = df['经营范围'].apply(find_kw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "tmp_df = df_kw[\n",
    "    df_kw.apply(lambda item: len(item) > 0)\n",
    "    ]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "kw_set = set()\n",
    "for item in tmp_df:\n",
    "    for it in item:\n",
    "        kw_set.add(it)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21660"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(kw_set)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 氢能黑名单结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "black_kw1 = [\n",
    "    \"氢氧\",\n",
    "    \"氧氢\",\n",
    "    \"氢胺\",\n",
    "    \"氯氢\",\n",
    "    \"硫氢\",\n",
    "    \"氢钠\",\n",
    "    \"氢酯\",\n",
    "    \"氢铵\",\n",
    "    \"酸氢\",\n",
    "    \"氢钾\",\n",
    "    \"脱氢\",\n",
    "    \"除氢\",\n",
    "    \"氢原子\",\n",
    "    \"芳氢\",\n",
    "    \"磷氢\",\n",
    "    \"碳氢\",\n",
    "    \"硅氢\",\n",
    "    \"氢化\",\n",
    "    \"化氢\",\n",
    "    \"聚氢\",\n",
    "    \"一氢\",\n",
    "    \"二氢\",\n",
    "    \"三氢\",\n",
    "    \"四氢\",\n",
    "    \"五氢\",\n",
    "    \"六氢\",\n",
    "    \"八氢\",\n",
    "    \"十氢\",\n",
    "    \"双氢\",\n",
    "    \"氢一\",\n",
    "    \"氢二\",\n",
    "    \"氢三\",\n",
    "    \"氢甲\",\n",
    "    \"氢乙\",\n",
    "    \"氢丙\",\n",
    "    \"氢丁\",\n",
    "    \"氢氟\",\n",
    "    \"氟氢\",\n",
    "    \"锡氢\",\n",
    "    \"异氢\",\n",
    "    \"氢尿\",\n",
    "    \"乙氢\",\n",
    "    \"甲氢\",\n",
    "    \"丙氢\",\n",
    "    \"丁氢\",\n",
    "    \"辛氢\",\n",
    "]\n",
    "\n",
    "black_kw2 = [\n",
    "    \"低氢\",\n",
    "    \"内氢\",\n",
    "    \"凤氢\",\n",
    "    \"数氢\",\n",
    "    \"无氢\",\n",
    "    \"氢不带\",\n",
    "    \"氢体\",\n",
    "    \"氢可酮\",\n",
    "    \"氢同位素\",\n",
    "    \"氢喃\",\n",
    "    \"氢基\",\n",
    "    \"氢安板\",\n",
    "    \"氢工纳\",\n",
    "    \"氢工铵\",\n",
    "    \"氢廢酸\",\n",
    "    \"氢弗\",\n",
    "    \"氢弹\",\n",
    "    \"氢所\",\n",
    "    \"氢气球\",\n",
    "    \"氢氨\",\n",
    "    \"氢氯\",\n",
    "    \"氢氰\",\n",
    "    \"氢溴\",\n",
    "    \"氢烃\",\n",
    "    \"氢烷\",\n",
    "    \"氢盐\",\n",
    "    \"氢砖\",\n",
    "    \"氢碘\",\n",
    "    \"氢离子\",\n",
    "    \"氢纳\",\n",
    "    \"氢纶\",\n",
    "    \"氢肥\",\n",
    "    \"氢肽\",\n",
    "    \"氢胶\",\n",
    "    \"氢脂\",\n",
    "    \"氢舱\",\n",
    "    \"氢苯\",\n",
    "    \"氢茎\",\n",
    "    \"氢菊\",\n",
    "    \"氢表雄\",\n",
    "    \"氢酸\",\n",
    "    \"氢醇\",\n",
    "    \"氢醌\",\n",
    "    \"氢钙\",\n",
    "    \"氢钢\",\n",
    "    \"氢降\",\n",
    "    \"氢露\",\n",
    "    \"氨氢\",\n",
    "    \"氮氢\",\n",
    "    \"环氢\",\n",
    "    \"石氢\",\n",
    "    \"砷华氢\",\n",
    "    \"硼氢过\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "black_kw = black_kw1 + black_kw2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "99"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(black_kw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "white_list = [\n",
    "    \"氢\", \"LNG\", \"CNG\", \"燃料电池\"\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "def filter_kw(item):\n",
    "    if not isinstance(item, str):\n",
    "        return False\n",
    "    for b_kw in black_kw:\n",
    "        item = item.replace(b_kw, \"\")\n",
    "    for w in white_list:\n",
    "        if w in item:\n",
    "            return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 经营范围获取氢能企业下标"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(8675611, 32)"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_jy = df[\"经营范围\"].apply(filter_kw)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 企业名称"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Index(['企业名称', '英文名称', '统一社会信用代码', '企业类型', '经营状态', '成立日期', '核准日期', '法定代表人',\n",
       "       '注册资本', '实缴资本', '参保人数', '公司规模', '经营范围', '注册地址', '营业期限', '纳税人识别号',\n",
       "       '工商注册号', '组织机构代码', '纳税人资质', '曾用名', '所属省份', '所属城市', '所属区县', '网站链接',\n",
       "       '所属行业', '一级行业分类', '二级行业分类', '三级行业分类', '登记机关', '经度', '纬度', '网址'],\n",
       "      dtype='object')"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "def filter_name(name):\n",
    "    if not isinstance(name, str):\n",
    "        return False\n",
    "    if \"氢\" in name:\n",
    "        return True\n",
    "    return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "df_name_idx = df[\"企业名称\"].apply(filter_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "idx = df_name_idx | df_jy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(51443, 32)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[idx].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[idx].to_csv(\"全国5万家氢能企业名单_utf8.csv\", index=False, encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "df[idx].to_csv(\"全国5万家氢能企业名单_gbk.csv\", index=False, encoding='utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(51443, 32)"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[idx].shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
